package com.health.fragment.focusgroup.child;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.health.viewscontrols.FWMYDPJActivity;
import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.PersonalFileActivity.onFragmentSave;
import com.health.R;
import com.health.adapter.CheckBoxAdapter;
import com.health.adapter.RadioButtonAdapter;
import com.health.db.FDbManager;
import com.health.viewscontrols.focusgroup.ChildActivity;
import com.health.fragment.NovaFragment;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.BD_DD_INFO;
import com.health.tablebean.CHILD_TCMHM_THREE2SIX;
import com.health.tablebean.CHILD_THREE2SIX_YEAR_OLD;
import com.health.tools.DateUtil;
import com.health.tools.GlobalConts;
import com.health.view.spinner.NiceSpinner;
import com.health.widget.EditTextWithDate;
import com.health.widget.MyGridView;
import com.novabluetooth.services.BluetoothConstant;
import com.novabluetooth.views.ExamHistoryActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * 3-6岁内检查 3岁健康检查
 *
 * @author 鹿云龙
 */
public class CheckFragment3_1 extends NovaFragment implements onFragmentSave, OnClickListener {
    private View baseView = null;
    private ARCHIVE_BASEINFO userInfo;// 基本信息
    /**
     * 未见异常
     */
    private List<BD_DD_INFO> lstyc;
    // private MyGridView mgvmianse;
    // private MyGridView mgvqianxin;
    private MyGridView mgvtingli;
    private MyGridView mgvxinfei;
    // CHILD_TCMHM_THREE2SIX
    // private MyGridView mgvpifu;
    // private MyGridView mgvkouqiang;
    private MyGridView mgvfubu;

    // 两次随访
    private MyGridView mgv_liangcisuifang;
    // 发育评估
    private MyGridView mgv_fayupinggu;
    /**
     * 指导mgv_zhidao
     */
    private MyGridView mgv_zhidao;
    // 转诊建议
    // private MyGridView mgv_zzjy;
    private MyGridView zhongyi;
    private NiceSpinner ns_tz;
    private NiceSpinner ns_sc;
    private NiceSpinner ns_sc_tz;
    private NiceSpinner ns_tgfypj;

    private EditText et_cy;// 出牙
    private EditText et_qc;// 龋齿

    private EditTextWithDate et_sfrq;
    private EditTextWithDate et_xcsfrq;
    private EditText et_tz;
    private EditText et_sc;
    private EditText et_sc_tz;
    // private EditText et_qt;
    private EditText ed_zzyy;
    private EditText ed_jg_ys;
    private EditText et_zyqt;
    private NiceSpinner et_sfysqm;// 随访医生

    private EditText et_hbfy;
    private EditText et_hbfx;
    private EditText et_hbws;
    private EditText et_hbqt;

    private EditText et_xhdbz;

    private static final Integer I_NULL = null;

    // SimpleDateFormat sdf = new Simp/leDateFormat("yyyy-MM-hh");

    private RadioGroup rd_zz;// 是否转诊
    private RadioButton rb_w;
    private RadioButton rb_y;
    private String str_zzId = "";

    private Button btn_tz_more;
    private Button btn_xhdb_more;
    List<EditText> lstTv;

    private int fromzy;
    private int from;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public View findViews(LayoutInflater inflater) {

        baseView = inflater.inflate(R.layout.child_check_fragment3_1, null);
        userInfo = NovaApplication.getUserInfo();

        setupView(baseView);

        // mgvmianse = (MyGridView) baseView.findViewById(R.id.mgvmianse);
        // mgvqianxin = (MyGridView) baseView.findViewById(R.id.mgvqianlu);
        mgvtingli = (MyGridView) baseView.findViewById(R.id.mgvtingli);
        mgvxinfei = (MyGridView) baseView.findViewById(R.id.mgvxinfei);

        // mgvpifu = (MyGridView) baseView.findViewById(R.id.mgvpifu);
        // mgvkouqiang = (MyGridView) baseView.findViewById(R.id.mgvkouqiang);
        mgvfubu = (MyGridView) baseView.findViewById(R.id.mgvfubu);
        mgv_liangcisuifang = (MyGridView) baseView.findViewById(R.id.mgv_liangcisuifang);
        mgv_fayupinggu = (MyGridView) baseView.findViewById(R.id.mgv_fayupinggu);
        mgv_zhidao = (MyGridView) baseView.findViewById(R.id.mgv_zhidao);
        // mgv_zzjy = (MyGridView) baseView.findViewById(R.id.mgv_zzjy);
        zhongyi = (MyGridView) baseView.findViewById(R.id.zhongyi);
        // mgv_fayupinggu
        String str = sdf.format(new Date(System.currentTimeMillis()));
        System.out.println(str);
        et_sfrq = (EditTextWithDate) baseView.findViewById(R.id.etw_sfrq);
        et_sfrq.setText(sdf.format(new Date(System.currentTimeMillis())));
        et_tz = (EditText) baseView.findViewById(R.id.et_tz);
        et_sc = (EditText) baseView.findViewById(R.id.et_sc);
        et_sc_tz = (EditText) baseView.findViewById(R.id.et_sc_tz);
        // et_pf = (EditText) baseView.findViewById(R.id.et_pf);
        // et_tl = (EditText) baseView.findViewById(R.id.et_tl);
        // et_kq = (EditText) baseView.findViewById(R.id.et_kq);
        // et_fb = (EditText) baseView.findViewById(R.id.et_fb);
        // et_hwhd = (EditText) baseView.findViewById(R.id.et_hwhd);
        // et_fywssd = (EditText) baseView.findViewById(R.id.et_fywssd);
        // et_qt = (EditText) baseView.findViewById(R.id.et_qt);
        // et_zzyy = (EditText) baseView.findViewById(R.id.et_zzyy);
        // et_jgjks = (EditText) baseView.findViewById(R.id.et_jgjks);
        et_zyqt = (EditText) baseView.findViewById(R.id.et_zyqt);
        et_sfysqm = (NiceSpinner) baseView.findViewById(R.id.et_sfysqm);
        GlobalConts.doctorList(et_sfysqm);
        et_xcsfrq = (EditTextWithDate) baseView.findViewById(R.id.et_xcsfrq);

        et_hbfy = (EditText) baseView.findViewById(R.id.et_hbfy);
        et_hbfx = (EditText) baseView.findViewById(R.id.et_hbfx);
        et_hbws = (EditText) baseView.findViewById(R.id.et_hbws);
        et_hbqt = (EditText) baseView.findViewById(R.id.et_hbqt);

        et_xhdbz = (EditText) baseView.findViewById(R.id.et_xhdbz);
        et_cy = (EditText) baseView.findViewById(R.id.et_cy);
        et_qc = (EditText) baseView.findViewById(R.id.et_qc);

        ns_tz = (NiceSpinner) baseView.findViewById(R.id.ns_tz);
        ns_sc = (NiceSpinner) baseView.findViewById(R.id.ns_sc);
        ns_sc_tz = (NiceSpinner) baseView.findViewById(R.id.ns_sc_tz);

        ns_tgfypj = (NiceSpinner) baseView.findViewById(R.id.ns_tgfypj);
        et_sfrq.setBefore(true);
        et_xcsfrq.setAfter(true);

        ed_zzyy = (EditText) baseView.findViewById(R.id.ed_zzyy);
        ed_jg_ys = (EditText) baseView.findViewById(R.id.ed_jg_ys);
        rd_zz = (RadioGroup) baseView.findViewById(R.id.rd_zz);
        rb_w = (RadioButton) baseView.findViewById(R.id.rb_w);
        rb_y = (RadioButton) baseView.findViewById(R.id.rb_y);
        ed_zzyy.setEnabled(false);
        ed_jg_ys.setEnabled(false);

        GlobalConts.setMaxLength(2, et_cy, et_qc);
        // GlobalConts.setMaxLength(50, et_qt);
        GlobalConts.setMaxLength(5, et_tz, et_sc, et_sc_tz);
        GlobalConts.setMaxLength(100, et_hbqt);
        GlobalConts.keep2decimal(et_tz, et_sc, et_sc_tz);

        bind();
        initData();
        return baseView;
    }

    private void initData() {


        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            // et_sfrq.setText(sdf.format(new Date()));
            // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-hh");

            CHILD_THREE2SIX_YEAR_OLD ctyo = new CHILD_THREE2SIX_YEAR_OLD();

            List<CHILD_THREE2SIX_YEAR_OLD> ctyos = FDbManager.getInstance(getActivity()).findAllByWhere(
                    CHILD_THREE2SIX_YEAR_OLD.class, "IDCARD = '" + userInfo.getIDCARD() + "'  AND FLAG='3'");

            if (ctyos.size() > 0) {

                ctyo = ctyos.get(ctyos.size() - 1);

                rbalc.setChecked(ctyo.getAMONG_TWO_FOLLO_NONE(), ctyo.getAMONG_TWO_FOLLO_OTHER());// rbalc.getChecked());
                if ("DM01-11_2".equals(ctyo.getAMONG_TWO_FOLLO_NONE())) {
                    for (int i = 0; i < lstTv.size(); i++) {
                        lstTv.get(i).setEnabled(true);
                        lstTv.get(i).setFocusable(true);
                        lstTv.get(i).setFocusableInTouchMode(true);
                        lstTv.get(i).setClickable(true);
                    }
                } else if ("DM01-11_1".equals(ctyo.getAMONG_TWO_FOLLO_NONE())) {
                    for (int i = 0; i < lstTv.size(); i++) {
                        lstTv.get(i).setEnabled(false);
                        lstTv.get(i).setFocusable(false);
                        lstTv.get(i).setFocusableInTouchMode(false);
                        lstTv.get(i).setClickable(false);
                    }
                }
                et_hbfx.setText(ctyo.getDIARRHOEA_FREQUEN());// strff.isEmpty()
                cbazhidao.setChecked(ctyo.getGUIDANCE());// cbazhidao.getCheckedIdsStr());
                cbazhidao.setOther(ctyo.getGUIDANCE_OTHER());// cbazhidao.getOther());
                rbaxf.setChecked(ctyo.getHEART_LUNG());// rbaxf.getChecked());
                String strXhdb = et_xhdbz.getText().toString();
                et_xhdbz.setText(ctyo.getHEMOGLOBIN_VALUE());// strXhdb.isEmpty()
                rbatl.setChecked(ctyo.getLISTENING());// rbatl.getChecked());
                try {
                    et_xcsfrq.setText(sdf.format(ctyo.getNEXT_FOLLOWUP_DATE()));// sdf.parse(et_xcsfrq.getText().toString()
                } catch (Exception e) {
                    e.printStackTrace();
                }
                // et_qt.setText(ctyo.getOTHER());//
                // et_qt.getText().toString());
                ns_tgfypj.setSelectedIndex(getListIndex(ctyo.getPHYSICAL_AUXE_EVALUAT(), lstFy));// .get(ns_tgfypj.getSelectedIndex()).g
                // String strfy = et_hbfy.getText().toString();
                et_hbfy.setText(ctyo.getPNEUMONIA_FREQUEN());// strfy.isEmpty()

                str_zzId = ctyo.getREFERRAL_ADVICE();

                if (!TextUtils.isEmpty(str_zzId)) {
                    if (str_zzId.equals("DM01-11_1")) {
                        rb_w.setChecked(true);
                    } else if (str_zzId.equals("DM01-11_2")) {
                        rb_y.setChecked(true);
                    }
                }

                // adapter_zz.setChecked(str_zzId);

                ed_zzyy.setText(ctyo.getREFERRAL_REASON());
                ed_jg_ys.setText(ctyo.getAGENCIES_DEPARTMENTS());

                et_sc.setText(ctyo.getSTATURE());// strsc.isEmpty()
                et_sc_tz.setText(ctyo.getSTATURE_WEIGHT());// 身高/体重比值
                ns_sc.setSelectedIndex(getListIndex(ctyo.getSTATURE_ANALYSIS(), lsttzsc));// .get(ns_sc.getSelectedIndex()).getID())
                ns_sc_tz.setSelectedIndex(getListIndex(ctyo.getSTATURE_WEIGHT_ANALYSIS(), lsttzsc));// .get(ns_sc.getSelectedIndex()).getID())
                rbafb.setChecked(ctyo.getSTOMACH());// rbafb.getChecked());
                rbafy.setChecked(ctyo.getAUXE_ESTIMATE_OTHER());// 发育评估
                // String strCy = et_cy.getText().toString();
                et_cy.setText(ctyo.getTCN_LEFT());// ;strCy.isEmpty()
                // String strQc = et_qc.getText().toString();
                et_qc.setText(ctyo.getTDCN_RIGHT());// strQc.isEmpty()
                // Integer.parseInt(strQc));
                // String strws = et_hbws.getText().toString();
                et_hbws.setText(ctyo.getTRAUMATISM_FREQUEN());// strws.isEmpty()
                // String strtz = et_tz.getText().toString();
                et_tz.setText(ctyo.getWEIGHT());// strtz.isEmpty()
                ns_tz.setSelectedIndex(getListIndex(ctyo.getWEIGHT_ANALYSIS(), lsttzsc));// .get(ns_tz.getSelectedIndex()).getID());
                et_hbqt.setText(ctyo.getAMONG_TWO_FOLLO_OTHER());
                // et_sfrq.setText(sdf.format(new Date()));
                for (int i = 0; i < NovaApplication.getDoctorList().size(); i++) {
                    if (ctyo.getFOLLOWUP_DOCTOR_SIGN().equals(NovaApplication.getDoctorList().get(i).getUSER_ID())) {
                        et_sfysqm.setSelectedIndex(i);
                    }
                }
            } else {
                // et_sfrq.setText(sdf.format(new Date()));
                for (int i = 0; i < NovaApplication.getDoctorList().size(); i++) {
                    if (userInfo.getCREATED_BY().equals(NovaApplication.getDoctorList().get(i).getUSER_ID())) {
                        et_sfysqm.setSelectedIndex(i);
                    }
                }
            }

            // TODO 中医药获取
            List<CHILD_TCMHM_THREE2SIX> ctts = FDbManager.getInstance(getActivity()).findAllByWhere(
                    CHILD_TCMHM_THREE2SIX.class, "IDCARD = '" + userInfo.getIDCARD() + "'  AND FOLLOWUP_TYPE='1'");
            if (ctts.size() > 0) {
                CHILD_TCMHM_THREE2SIX ctt = ctts.get(ctts.size() - 1);

                cbazy.setChecked(ctt.getTCMHM());
                cbazy.setOther(ctt.getTCMHM_OTHER());
                if (fromzy == 0) {
                    FDbManager.getInstance(getActivity()).save(ctt);
                    fromzy = 1;
                } else {
                    FDbManager.getInstance(getActivity()).updata(ctt,
                            "IDCARD='" + userInfo.getIDCARD() + "'  AND FOLLOWUP_TYPE='1'");
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    RadioButtonAdapter rbaxf;
    RadioButtonAdapter rbatl;
    RadioButtonAdapter rbapf;
    CheckBoxAdapter cbazhidao;
    RadioButtonAdapter rbalc;
    CheckBoxAdapter rbafy;
    // RadioButtonAdapter rbazz;
    RadioButtonAdapter rbakq;
    RadioButtonAdapter rbafb;
    List<BD_DD_INFO> lsttzsc;
    List<BD_DD_INFO> lstFy;
    CheckBoxAdapter cbazy;

    public void bind() {
        /*获取异常和未见异常值*/
        lstyc = GlobalConts.getDictionaryValue(getActivity(), "DM01-40");

        // 面色DM05-14_1
        List<BD_DD_INFO> lstms = FDbManager.getInstance(getActivity()).findAllByWhereInInfoDB(BD_DD_INFO.class,
                "PARENTID = 'DM05-14_1' order by cast(SORT as int) asc");
        RadioButtonAdapter rbamianse = new RadioButtonAdapter(lstms, getActivity());
        rbamianse.setOtherTextVisible(false);
        // mgvmianse.setAdapter(rbamianse);
        // 听力DM01-30
        List<BD_DD_INFO> lsttl = FDbManager.getInstance(getActivity()).findAllByWhereInInfoDB(BD_DD_INFO.class,
                "PARENTID = 'DM01-30' order by cast(SORT as int) asc");
        rbatl = new RadioButtonAdapter(lsttl, getActivity());
        rbatl.setOtherTextVisible(false);
        mgvtingli.setAdapter(rbatl);
        // 心肺lstyc
        rbaxf = new RadioButtonAdapter(lstyc, getActivity());
        rbaxf.setOtherTextVisible(false);
        mgvxinfei.setAdapter(rbaxf);
        // mgvpifu
        rbapf = new RadioButtonAdapter(lstyc, getActivity());
        rbapf.setOtherTextVisible(false);
        // mgvpifu.setAdapter(rbapf);
        // 口腔
        rbakq = new RadioButtonAdapter(lstyc, getActivity());
        rbakq.setOtherTextVisible(false);
        // mgvkouqiang.setAdapter(rbakq);
        // 腹部
        rbafb = new RadioButtonAdapter(lstyc, getActivity());
        rbafb.setOtherTextVisible(false);
        mgvfubu.setAdapter(rbafb);
        // 两次随访DM05-20
        List<BD_DD_INFO> lstlc = FDbManager.getInstance(getActivity()).findAllByWhereInInfoDB(BD_DD_INFO.class,
                "PARENTID = 'DM01-11' order by cast(SORT as int) asc");
        lstTv = new ArrayList<EditText>();
        lstTv.add(et_hbfy);
        lstTv.add(et_hbfx);
        lstTv.add(et_hbws);
        lstTv.add(et_hbqt);
        rbalc = new RadioButtonAdapter(lstlc, getActivity(), lstTv);
        rbalc.setOtherTextVisible(false);
        mgv_liangcisuifang.setAdapter(rbalc);

        // 发育评估 DM01-30
        List<BD_DD_INFO> lstfy = FDbManager.getInstance(getActivity()).findAllByWhereInInfoDB(BD_DD_INFO.class,
                "PARENTID = 'DM05-26_8' order by cast(SORT as int) asc");
        rbafy = new CheckBoxAdapter(getActivity(), lstfy);
        rbafy.setOtherVis(false);
        mgv_fayupinggu.setAdapter(rbafy);
        // 健康指导
        List<BD_DD_INFO> lstzd = FDbManager.getInstance(getActivity()).findAllByWhereInInfoDB(BD_DD_INFO.class,
                "PARENTID = 'DM05-04' order by cast(SORT as int) asc");
        List<BD_DD_INFO> listZZs_new = new ArrayList<BD_DD_INFO>();
        int size = lstzd.size();
        if (size > 0) {
            for (int i = 0; i < size; i++) {

                // DM05-04_1 科学喂养 ；DM05-04_2 合理膳食 ；DM05-04_3 生长发育
                // DM05-04_4 疾病预防;DM05-04_5 预防意外伤害;DM05-04_6 口腔保健
                // DM05-04_9 其他
                if (lstzd.get(i).getID().equals("DM05-04_2") || lstzd.get(i).getID().equals("DM05-04_3")
                        || lstzd.get(i).getID().equals("DM05-04_4") || lstzd.get(i).getID().equals("DM05-04_5")
                        || lstzd.get(i).getID().equals("DM05-04_6") || lstzd.get(i).getID().equals("DM05-04_9")) {
                    listZZs_new.add(lstzd.get(i));
                }
            }
        }

        cbazhidao = new CheckBoxAdapter(getActivity(), listZZs_new);
        mgv_zhidao.setAdapter(cbazhidao);
        // 转诊建议
        // rbazz = new RadioButtonAdapter(lstyw, getActivity());
        // mgv_zzjy.setAdapter(rbazz);

//         /*中医DM06-06*/
//        List<BD_DD_INFO> lstzy = FDbManager.getInstance(getActivity()).findAllByWhereInInfoDB(BD_DD_INFO.class,
//                "PARENTID = 'DM06-06' order by cast(SORT as int) asc");
//
//        List<BD_DD_INFO> listZZs_new_zy = new ArrayList<BD_DD_INFO>();
//        int size_zy = lstzy.size();
//        if (size_zy > 0) {
//            for (int i = 0; i < size_zy; i++) {
//
//                // DM06-06_5 5 按揉迎香穴、足三里穴
//                // DM06-06_6 6 按揉四神聪穴
//                // DM06-06_1 1 饮食调养指导
//                // DM06-06_2 2 起居调摄指导
//                // DM06-06_3 3 摩腹捏脊
//                // DM06-06_4 4 健康教育处方
//                if (lstzy.get(i).getID().equals("DM06-06_1") || lstzy.get(i).getID().equals("DM06-06_2")
//                        || lstzy.get(i).getID().equals("DM06-06_6") || lstzy.get(i).getID().equals("DM06-06_7")) {
//                    listZZs_new_zy.add(lstzy.get(i));
//                }
//            }
//        }
        /*中医DM200-04*/
        List<BD_DD_INFO> listzyNew = new ArrayList<>();
        List<BD_DD_INFO> lstzy = GlobalConts.getDictionaryValue(getActivity(), "DM200-04");
        if (lstzy.size() > 0) {
            for (BD_DD_INFO item : lstzy) {
                /**
                 * DM200-04_1：中医饮食调养指导
                 * DM200-04_2：中医起居调摄指导
                 * DM200-04_5：传授按揉四神聪穴方法
                 * DM200-04_0：其他
                 */
                switch (item.getID()) {
                    case "DM200-04_0":
                    case "DM200-04_1":
                    case "DM200-04_2":
                    case "DM200-04_5":
                        listzyNew.add(item);
                        break;
                    default:
                }
            }
        }
        cbazy = new CheckBoxAdapter(getActivity(), listzyNew);
        zhongyi.setAdapter(cbazy);
        // 体重DM01-39
        lsttzsc = FDbManager.getInstance(getActivity()).findAllByWhereInInfoDB(BD_DD_INFO.class,
                "PARENTID = 'DM01-39' order by cast(SORT as int) asc");
        ns_tz.attachDataSource(getDataInfo(lsttzsc));
        // 身长DM01-39
        ns_sc.attachDataSource(getDataInfo(lsttzsc));
        ns_sc_tz.attachDataSource(getDataInfo(lsttzsc));
        // 体格发育评价DM05-03
        lstFy = FDbManager.getInstance(getActivity()).findAllByWhereInInfoDB(BD_DD_INFO.class,
                "PARENTID = 'DM05-03' order by cast(SORT as int) asc");
        ns_tgfypj.attachDataSource(getDataInfo(lstFy));
    }

    /**
     * 获取下拉框中需要的数据的信息
     *
     * @param datas
     * @return
     * @author 鹿云龙
     * @date 2015年12月16日 下午3:47:00
     */
    private ArrayList<String> getDataInfo(List<BD_DD_INFO> datas) {
        ArrayList<String> infos = new ArrayList<String>();
        for (BD_DD_INFO bd : datas) {
            infos.add(bd.getDATAINFO());
        }
        return infos;
    }

    @Override
    public void setupView(View view) {

        // mgvmianse = (MyGridView) baseView.findViewById(R.id.mgvmianse);
        // mgvqianxin = (MyGridView) baseView.findViewById(R.id.mgvqianlu);
        mgvtingli = (MyGridView) baseView.findViewById(R.id.mgvtingli);
        mgvxinfei = (MyGridView) baseView.findViewById(R.id.mgvxinfei);

        // mgvpifu = (MyGridView) baseView.findViewById(R.id.mgvpifu);
        // mgvkouqiang = (MyGridView) baseView.findViewById(R.id.mgvkouqiang);
        mgvfubu = (MyGridView) baseView.findViewById(R.id.mgvfubu);
        mgv_liangcisuifang = (MyGridView) baseView.findViewById(R.id.mgv_liangcisuifang);
        mgv_zhidao = (MyGridView) baseView.findViewById(R.id.mgv_zhidao);
        // mgv_zzjy = (MyGridView) baseView.findViewById(R.id.mgv_zzjy);
        zhongyi = (MyGridView) baseView.findViewById(R.id.zhongyi);
        // mgv_fayupinggu
        String str = sdf.format(new Date(System.currentTimeMillis()));
        System.out.println(str);
        et_sfrq = (EditTextWithDate) baseView.findViewById(R.id.etw_sfrq);
        et_sfrq.setText(sdf.format(new Date(System.currentTimeMillis())));
        et_tz = (EditText) baseView.findViewById(R.id.et_tz);
        et_sc = (EditText) baseView.findViewById(R.id.et_sc);
        et_sc_tz = (EditText) baseView.findViewById(R.id.et_sc_tz);
        // et_pf = (EditText) baseView.findViewById(R.id.et_pf);
        // et_tl = (EditText) baseView.findViewById(R.id.et_tl);
        // et_kq = (EditText) baseView.findViewById(R.id.et_kq);
        // et_fb = (EditText) baseView.findViewById(R.id.et_fb);
        // et_hwhd = (EditText) baseView.findViewById(R.id.et_hwhd);
        // et_fywssd = (EditText) baseView.findViewById(R.id.et_fywssd);
        // et_qt = (EditText) baseView.findViewById(R.id.et_qt);
        // et_zzyy = (EditText) baseView.findViewById(R.id.et_zzyy);
        // et_jgjks = (EditText) baseView.findViewById(R.id.et_jgjks);
        et_zyqt = (EditText) baseView.findViewById(R.id.et_zyqt);
        et_sfysqm = (NiceSpinner) baseView.findViewById(R.id.et_sfysqm);
        GlobalConts.doctorList(et_sfysqm);
        et_xcsfrq = (EditTextWithDate) baseView.findViewById(R.id.et_xcsfrq);

        et_hbfy = (EditText) baseView.findViewById(R.id.et_hbfy);
        et_hbfx = (EditText) baseView.findViewById(R.id.et_hbfx);
        et_hbws = (EditText) baseView.findViewById(R.id.et_hbws);
        et_hbqt = (EditText) baseView.findViewById(R.id.et_hbqt);

        et_xhdbz = (EditText) baseView.findViewById(R.id.et_xhdbz);
        et_cy = (EditText) baseView.findViewById(R.id.et_cy);
        et_qc = (EditText) baseView.findViewById(R.id.et_qc);

        ns_tz = (NiceSpinner) baseView.findViewById(R.id.ns_tz);
        ns_sc = (NiceSpinner) baseView.findViewById(R.id.ns_sc);
        ns_sc_tz = (NiceSpinner) baseView.findViewById(R.id.ns_sc_tz);

        ns_tgfypj = (NiceSpinner) baseView.findViewById(R.id.ns_tgfypj);
        et_sfrq.setBefore(true);
        et_xcsfrq.setAfter(true);

        ed_zzyy = (EditText) baseView.findViewById(R.id.ed_zzyy);
        ed_jg_ys = (EditText) baseView.findViewById(R.id.ed_jg_ys);
        rd_zz = (RadioGroup) baseView.findViewById(R.id.rd_zz);
        rb_w = (RadioButton) baseView.findViewById(R.id.rb_w);
        rb_y = (RadioButton) baseView.findViewById(R.id.rb_y);
        ed_zzyy.setEnabled(false);
        ed_jg_ys.setEnabled(false);

        btn_tz_more = (Button) baseView.findViewById(R.id.btn_tz_more);
        btn_tz_more.setOnClickListener(this);
        btn_xhdb_more = (Button) baseView.findViewById(R.id.btn_xhdb_more);
        btn_xhdb_more.setOnClickListener(this);

    }

    @Override
    public void addListener() {
        rd_zz.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_w) {
                    str_zzId = "DM01-11_1";
                    ed_zzyy.setText("");
                    ed_jg_ys.setText("");
                    ed_zzyy.setEnabled(false);
                    ed_jg_ys.setEnabled(false);

                } else if (checkedId == R.id.rb_y) {
                    str_zzId = "DM01-11_2";
                    ed_zzyy.setEnabled(true);
                    ed_jg_ys.setEnabled(true);
                } else {
                    str_zzId = "";
                }

            }
        });
    }

    @Override
    public void setData() {
        setDateCheck();

    }

    private void setDateCheck() {

        // 出牙
        GlobalConts.checkNumOrange(getActivity(), et_cy, 32, 0);
        // 出牙
        GlobalConts.checkNumOrange(getActivity(), et_qc, 32, 0);
        // 肺炎
        GlobalConts.checkNumOrange(getActivity(), et_hbfy, 99, 0);
        // 腹泻
        GlobalConts.checkNumOrange(getActivity(), et_hbfx, 99, 0);
        // 外伤
        GlobalConts.checkNumOrange(getActivity(), et_hbws, 99, 0);

    }

    public int getListIndex(String id, List<BD_DD_INFO> bds) {
        int index = 0;
        try {
            for (int i = 0; i < bds.size(); i++) {
                if (id.equals(bds.get(i).getID())) {
                    index = i;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return index;
    }

    /**
     * 字符串转换"1,2,3,4,5" --> "'1','2','3','4','5'"
     *
     * @param symptom
     * @return
     * @author 鹿云龙
     * @date 2015年12月19日 下午2:49:23
     */
    public String checkedIdsSql(String symptom) {
        String[] idarr = symptom.split(",");
        String sql = "";
        for (String id : idarr) {
            sql += "'" + id + "',";
        }
        sql = sql.substring(0, sql.length() - 1);
        System.out.println(sql);
        return sql;
    }

    @Override
    public void save() {
        int age = DateUtil.getAge(userInfo.getBIRTHDAY());
        if (age < 3) {
            ((ChildActivity) getActivity()).showTextDialog("儿童未满3岁，不予保存");
            return;
        }

        if (!GlobalConts.getCheckNumOrangeBoolean(getActivity(), et_cy, 32, 0)) {
            ((ChildActivity) getActivity()).showTextDialog("出牙数不合理,最大为32颗，保存失败");
            return;
        }
        if (!GlobalConts.getCheckNumOrangeBoolean(getActivity(), et_qc, 32, 0)) {
            ((ChildActivity) getActivity()).showTextDialog("龋齿数不合理，最大为32颗，保存失败");
            return;
        }
        if (!GlobalConts.getCheckNumOrangeBoolean(getActivity(), et_hbfy, 99, 0)) {
            ((ChildActivity) getActivity()).showTextDialog("肺炎数据不合理，保存失败");
            return;
        }
        if (!GlobalConts.getCheckNumOrangeBoolean(getActivity(), et_hbfx, 99, 0)) {
            ((ChildActivity) getActivity()).showTextDialog("腹泻数据不合理，保存失败");
            return;
        }
        if (!GlobalConts.getCheckNumOrangeBoolean(getActivity(), et_hbws, 99, 0)) {
            ((ChildActivity) getActivity()).showTextDialog("外伤不合理，保存失败");
            return;
        }
        saveData();
    }

    private void saveData() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // CHILD_THREE2SIX_YEAR_OLD
        CHILD_THREE2SIX_YEAR_OLD ctyo = new CHILD_THREE2SIX_YEAR_OLD();

        if (rb_w.isChecked()) {
            str_zzId = "DM01-11_1";
        } else if (rb_y.isChecked()) {
            str_zzId = "DM01-11_2";
        } else {
            str_zzId = "";
        }
        ctyo.setREFERRAL_ADVICE(str_zzId);
        ctyo.setREFERRAL_REASON(ed_zzyy.getText().toString());
        ctyo.setAGENCIES_DEPARTMENTS(ed_jg_ys.getText().toString());

        ctyo.setAMONG_TWO_FOLLO_NONE(rbalc.getChecked());
        ctyo.setAMONG_TWO_FOLLO_OTHER(et_hbqt.getText().toString());
        // ctyo.setARCHIVEID(aRCHIVEID);//档案编号
        // ctyo.setCREATED_BY(cREATED_BY);
        ctyo.setCREATED_DATE(new Date());
        // ctyo.setCUSTOMERID(cUSTOMERID);//客户编号
        String strff = et_hbfx.getText().toString();
        // if (!strff.isEmpty()) {
        ctyo.setDIARRHOEA_FREQUEN(strff);
        // }
        ctyo.setFLAG("3");
        ctyo.setFOLLOWUP_DATE(new Date());
        ctyo.setFOLLOWUP_DOCTOR_SIGN(NovaApplication.getDoctorList().get(et_sfysqm.getSelectedIndex()).getUSER_ID());
        ctyo.setGUIDANCE(cbazhidao.getCheckedIdsStr());
        ctyo.setGUIDANCE_OTHER(cbazhidao.getOther());
        ctyo.setAUXE_ESTIMATE_OTHER(rbafy.getCheckedIdsStr());
        ctyo.setHEART_LUNG(rbaxf.getChecked());
        String strXhdb = et_xhdbz.getText().toString();
        // if (!strXhdb.isEmpty()) {
        ctyo.setHEMOGLOBIN_VALUE(strXhdb);
        // } // ctyo.setID(iD);
        ctyo.setIDCARD(userInfo.getIDCARD());
        // ctyo.setIS_DEL(iS_DEL);
        // ctyo.setLAST_UPDATE_BY(lAST_UPDATE_BY);
        ctyo.setLAST_UPDATE_DATE(new Date());
        ctyo.setLISTENING(rbatl.getChecked());
        // ctyo.setNAME(nAME);
        try {
            ctyo.setNEXT_FOLLOWUP_DATE(sdf.parse(et_xcsfrq.getText().toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        // ctyo.setOTHER(et_qt.getText().toString());
        ctyo.setPHYSICAL_AUXE_EVALUAT(lstFy.get(ns_tgfypj.getSelectedIndex()).getID());// 体格发育评价1正常
        String strfy = et_hbfy.getText().toString();
        // if (!strfy.isEmpty()) {
        ctyo.setPNEUMONIA_FREQUEN(strfy);
        // }

        // TODO s视力
        // ctyo.setSIGHT("3");
        String strsc = et_sc.getText().toString();
        // String strsc_tz = et_sc_tz.getText().toString();
        // if (!strsc.isEmpty()) {
        ctyo.setSTATURE(strsc);

        ctyo.setSTATURE_WEIGHT(et_sc_tz.getText().toString().trim());
        // ctyo.setSTATURE(strsc_tz);
        // }
        ctyo.setSTATURE_ANALYSIS(lsttzsc.get(ns_sc.getSelectedIndex()).getID());// 身长上中下
        ctyo.setSTATURE_WEIGHT_ANALYSIS(lsttzsc.get(ns_sc_tz.getSelectedIndex()).getID());
        // ctyo.setSTATURE_ANALYSIS(lsttzsc.get(ns_sc_tz.getSelectedIndex()).getID());//
        // 身长上中下
        ctyo.setSTOMACH(rbafb.getChecked());
        String strCy = et_cy.getText().toString();
        // if (!strCy.isEmpty()) {
        ctyo.setTCN_LEFT(strCy);
        // }
        String strQc = et_qc.getText().toString();
        // if (!strQc.isEmpty()) {
        ctyo.setTDCN_RIGHT(strQc);
        // }
        String strws = et_hbws.getText().toString();
        // if (!strws.isEmpty()) {
        ctyo.setTRAUMATISM_FREQUEN(strws);
        // }
        String strtz = et_tz.getText().toString();
        // if (strtz.isEmpty()) {
        ctyo.setWEIGHT(strtz);
        // }
        ctyo.setWEIGHT_ANALYSIS(lsttzsc.get(ns_tz.getSelectedIndex()).getID());
        ctyo.setIS_UPDATE("Y");
        try {
            ctyo.setFOLLOWUP_DATE(sdf.parse(et_sfrq.getText().toString()));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ctyo.setLAST_UPDATE_BY(NovaApplication.getDoctorList().get(et_sfysqm.getSelectedIndex()).getUSER_ID());
        ctyo.setLAST_UPDATE_DATE(new Date());
        if (from == 0) {

            ctyo.setCREATED_BY(userInfo.getCREATED_BY());
            ctyo.setCREATED_DATE(new Date());
            ctyo.setCREATEORG(userInfo.getCREATEORG());
            ctyo.setCREATER(userInfo.getCREATER());

            FDbManager.getInstance(getActivity()).save(ctyo);
            from = 1;
        } else {
            ctyo.setUPDATEORG(userInfo.getCREATEORG());
            ctyo.setMODIFIED(NovaApplication.getDoctorList().get(et_sfysqm.getSelectedIndex()).getUSER_ID());
            FDbManager.getInstance(getActivity()).updata(ctyo, "IDCARD='" + userInfo.getIDCARD() + "'  AND FLAG='3'");
        }
        // TODO 中医药保存
        CHILD_TCMHM_THREE2SIX ctt = new CHILD_TCMHM_THREE2SIX();
        ctt.setARCHIVEID("");
        ctt.setIDCARD(userInfo.getIDCARD());
        ctt.setFOLLOWUP_DOCTOR(NovaApplication.getDoctorList().get(et_sfysqm.getSelectedIndex()).getUSER_ID());
        ctt.setFOLLOWUP_TYPE("1");
        ctt.setTCMHM(cbazy.getCheckedIdsStr());
        ctt.setTCMHM_OTHER(cbazy.getOther());
        ctt.setLAST_UPDATE_BY(NovaApplication.getDoctorList().get(et_sfysqm.getSelectedIndex()).getUSER_ID());
        ctt.setLAST_UPDATE_DATE(new Date());
        if (fromzy == 0) {
            ctt.setCREATED_BY(userInfo.getCREATED_BY());
            ctt.setCREATED_DATE(new Date());
            ctt.setCREATEORG(userInfo.getCREATEORG());
            ctt.setCREATER(userInfo.getCREATER());
            FDbManager.getInstance(getActivity()).save(ctt);
            fromzy = 1;
        } else {

            ctt.setUPDATEORG(userInfo.getCREATEORG());
            ctt.setMODIFIED(NovaApplication.getDoctorList().get(et_sfysqm.getSelectedIndex()).getUSER_ID());
            FDbManager.getInstance(getActivity()).updata(ctt,
                    "IDCARD='" + userInfo.getIDCARD() + "'  AND FOLLOWUP_TYPE='1'");
        }
        // ((ChildActivity) getActivity()).showTextDialog("保存成功");
        if (GlobalConts.TYPE_JTYS.equals(NovaApplication.getType())) {
            new AlertDialog.Builder(getActivity()).setTitle("保存成功").setMessage("进行满意度评价")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(getContext(), FWMYDPJActivity.class);
                            intent.putExtra("type", "02");
                            intent.putExtra("szfs", "");
                            startActivity(intent);
                        }
                    }).show();
        } else if (GlobalConts.TYPE_GJJBGGWS.equals(NovaApplication.getType())) {
            ((ChildActivity) getActivity()).showTextDialog("保存成功");
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(), ExamHistoryActivity.class);
        int id = v.getId();
        if (id == R.id.btn_tz_more) {
            intent.putExtra("flag", BluetoothConstant.TIZHONG_TYPENAME);
            getActivity().startActivityForResult(intent, GlobalConts.RequestTizhong);
        } else if (id == R.id.btn_xhdb_more) {
            intent.putExtra("flag", "血红蛋白");
            getActivity().startActivityForResult(intent, GlobalConts.RequestXueHongDanbai);
        } else {
        }
    }

    @Override
    public void setTizhong(String value) {
        et_tz.setText(value);

    }

    @Override
    public void setXueHongDanBai(String xhdb) {
        if (!TextUtils.isEmpty(xhdb)) {
            et_xhdbz.setText(String.valueOf((int) (Double.parseDouble(xhdb) * 10)));
        }
    }
}